<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Interact iRedAdmin-Pro RESTful API with Python</title>
        <link rel="stylesheet" type="text/css" href="./css/markdown.css" />
    </head>
    <body>

    <div id="navigation">
    <a href="https://www.iredmail.org" target="_blank">
        <img alt="iRedMail web site"
             src="./images/logo-iredmail.png"
             style="vertical-align: middle; height: 30px;"
             />&nbsp;
        <span>iRedMail</span>
    </a>
    &nbsp;&nbsp;//&nbsp;&nbsp;<a href="./index.html">Document Index</a></div><h1 id="interact-iredadmin-pro-restful-api-with-python">Interact iRedAdmin-Pro RESTful API with Python</h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul>
<li>For more details about iRedAdmin-Pro RESTful API, please read document:
  <a href="./iredadmin-pro.restful.api.html">iRedAdmin-Pro: RESTful API</a>.</li>
<li>If you need an API which has not yet been implemented, don't hesitate to
  <a href="https://www.iredmail.org/contact.html">contact us</a>.</li>
<li>Our sample code below requires third-party Python module <code>requests</code>. For
  more details, please read its official documentation:
  <a href="https://docs.python-requests.org/en/master/">Requests: HTTP for Humans</a></li>
</ul>
</div>
<p>Sample Python code to interact iRedAdmin-Pro RESTful API.</p>
<pre><code>import sys
import requests

url = 'https://&lt;server&gt;/iredadmin/api'

# Admin email address and password.
admin = 'postmaster@mydomain.com'
pw = 'my_password'

# Login
r = requests.post(url + '/login', data={'username': admin,
                                        'password': pw})

# Get returned JSON data
data = r.json()
if not data['_success']:
    sys.exit('Login failed')

cookies = r.cookies

# Create domain: test.com
requests.post(url + '/domain/test.com',
              cookies=cookies,
              data={'defaultQuota': '1024'})

# Create user: zhb@test.com
requests.post(url + '/user/zhb@test.com',
              cookies=cookies,
              data={'cn': 'My Name',
                    'password': 'A@StrongPassword2',
                    'preferredLanguage': 'zh_CN',
                    'mailQuota': 2048})

# Create list: list@test.com. Note: OpenLDAP only.
requests.post(url + '/maillist/list@test.com',
              cookies=cookies,
              data={'cn': 'My List'})

# Create alias: alias@test.com
requests.post(url + '/alias/alias@test.com',
              cookies=cookies,
              data={'cn': 'My Alias'})

# Update user: zhb@test.com
requests.put(url + '/user/zhb@test.com',
             cookies=cookies,
             data={'cn': 'My New Name',
                   'password': 'WHDZ2@5yxORvVqzYY',
                   'transport': 'dovecot',
                   'language': 'en_US',
                   'quota': 2048})

# Delete user: zhb@test.com
requests.delete(url + '/user/zhb@test.com', cookies=cookies)

# Delete mailing list: list@test.com. Note: OpenLDAP only.
requests.delete(url + '/maillist/list@test.com', cookies=cookies)

# Delete alias: alias@test.com
requests.delete(url + '/alias/alias@test.com', cookies=cookies)

# Delete domain: test.com
requests.delete(url + '/domain/test.com', cookies=cookies)
</code></pre>
<h2 id="see-also">See Also</h2>
<ul>
<li><a href="./iredadmin-pro.restful.api.curl.html">Interact iRedAdmin-Pro RESTful API with <code>curl</code></a></li>
</ul><div class="footer">
    <p style="text-align: center; color: grey;">All documents are available in <a href="https://github.com/iredmail/docs/">GitHub repository</a>, and published under <a href="http://creativecommons.org/licenses/by-nd/3.0/us/" target="_blank">Creative Commons</a> license. You can <a href="https://github.com/iredmail/docs/archive/master.zip">download the latest version</a> for offline reading. If you found something wrong, please do <a href="https://www.iredmail.org/contact.html">contact us</a> to fix it.</p>
</div></body></html>